package socketVersion.Backend.Customer;

import java.util.LinkedList;
import java.util.Random;
import java.util.Vector;

import socketVersion.Backend.Cars.CarServerWrapper;
import socketVersion.Backend.Flights.FlightServerWrapper;
import socketVersion.Backend.Rooms.RoomServerWrapper;


public class CustomerServerImplementation implements CustomerInterface{
	
	private LinkedList<Integer> customers;
	private CarServerWrapper remoteToCars;
	private FlightServerWrapper remoteToFlights;
	private RoomServerWrapper remoteToRooms;
	
	public CustomerServerImplementation(String cars,String flights,String rooms){
		customers = new LinkedList<Integer>();
		
		String[] carIp = cars.split(":");
		String[] flightIp = flights.split(":");
		String[] roomIp = rooms.split(":");
		
		remoteToCars = new CarServerWrapper(carIp[0],Integer.parseInt(carIp[1]));
		remoteToFlights = new FlightServerWrapper(flightIp[0],Integer.parseInt(flightIp[1]));
		remoteToRooms = new RoomServerWrapper(roomIp[0],Integer.parseInt(roomIp[1]));
	}
	
	public int newCustomer(int id){
		System.out.println("Creating new customer (no id)");
		Random generator = new Random();
		int newId;
		while(true){
			newId = generator.nextInt(Integer.MAX_VALUE);
			if(customers.contains((Integer)newId)){
				continue;
			}
			else{
				break;
			}
		}
		
		customers.add(newId);
		return newId;
	}
	
	public boolean newCustomer(int id, int cid){
		System.out.println("Creating new customer cid: " + cid);
		if(customers.contains((Integer)cid)){
			return false;
		}
		else{
			customers.add((Integer)cid);
			return true;
		}
		
	}
	
	public boolean deleteCustomer(int id,int customer){
		System.out.println("Deleting customer: (" + id + "," + customer + ")");
		if(customers.contains((Integer)customer)){
			customers.remove((Integer)customer);
			if(!remoteToCars.deleteReservation(id, customer)){
				return false;
			}
			if(!remoteToRooms.deleteReservation(id, customer)){
				return false;
			}
			if(!remoteToFlights.deleteReservation(id, customer)){
				return false;
			}
			return true;
		}
		else{
			return true;
		}
		
	}
	
	public String queryCustomerInfo(int id,int customer){
		System.out.println("Querying customer: (" + id + "," + customer + ")");
		if(!customers.contains(customer)){
			return "No customer found with id: " + customer;
		}
		
		String bill = "";
		int roomsTotal = remoteToRooms.calculateBill(id, customer);
		int carsTotal = remoteToCars.calculateBill(id, customer);
		int flightsTotal = remoteToFlights.calculateBill(id, customer);
		int totalTotal = roomsTotal + carsTotal + flightsTotal;
		if(roomsTotal == -1 || carsTotal == -1 || flightsTotal == -1){
			return "An error occured while calculating the bill for customer: " + customer;
		}
		
		bill = "Bill for customer: " + customer + "\n";
		bill = bill + "Total amount for cars = " + carsTotal + "\n";
		bill = bill + "Total amount for rooms = " + roomsTotal + "\n";
		bill = bill + "Total amount for flights = " + flightsTotal + "\n";
		bill = bill + "Total: " + totalTotal;
		return bill;
		
		
	}

	@Override
	public boolean itinerary(int id, int customer, Vector flightNumbers,
			String location, boolean car, boolean room) {
		
		System.out.println("Creating itinerary for customer: (" + id + "," + customer + ")");
		System.out.println("At location: " + location);
		
		
		if(!customers.contains(customer)){
			System.out.println("Customer: " + customer + " does not exist");
			return false;
		}
		
		if(flightNumbers!=null){
			for(int i = 0;i<flightNumbers.size();i++){
				int flight = Integer.parseInt((String)flightNumbers.get(i));
				boolean success = remoteToFlights.reserveFlight(id, customer, flight);
				if(success==false){
					System.out.println("Could not reserve flight: " + flight);
					return false;
				}
			}
		}
		
		if(car){
			boolean success = remoteToCars.reserveCar(id, customer, location);
			if(success==false){
				System.out.println("Could not reserve car in: " + location);
				return false;
			}
		}
		
		if(room){
			boolean success = remoteToRooms.reserveRoom(id, customer, location);
			if(success==false){
				System.out.println("Could not reserve room in: " + location);
				return false;
			}
		}
		
		
		return true;
	}
}
